package leetcode_数组.leetcode_数组的旋转;

// 旋转函数
public class _396 {
    public static void main(String[] args) {
        int[] nums = {4, 3, 2, 6};
        System.out.println(maxRotateFunction(nums));
    }

    public static int maxRotateFunction(int[] A) {
        int ans = 0;
        for (int i = 0; i < A.length; i++) {
            ans += i * A[i];
        }
        for (int i = 0; i < A.length; i++) {
            int tem = 0;
            if (i != 0) {
                rotate(A, 1);
            }
            for (int j = 0; j < A.length; j++) {
                tem += j * A[j];
            }
            ans = Math.max(ans, tem);
        }
        return ans;
    }

    public static void rotate(int[] nums, int k) {
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
    }

    public static void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int tem = nums[start];
            nums[start] = nums[end];
            nums[end] = tem;
            start++;
            end--;
        }
    }

}
